package com.example.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.bean.Film;
import com.example.bean.MovieInfo;
import com.example.bean.SpecialZone;
import com.example.doubanfilm.R;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewAdapterForMain extends BaseQuickAdapter<SpecialZone, BaseViewHolder> {
    private Context context;


    public RecyclerViewAdapterForMain(int layoutResId, @Nullable List<SpecialZone> data,Context context) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, SpecialZone item) {
        helper.setText(R.id.zoneName,item.getZoneName())
              .setText(R.id.more,item.getMore())
              .addOnClickListener(R.id.more);

        RecyclerView recyclerView = helper.getView(R.id.recyclerView_zone);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);

        RecyclerViewAdapterForMainitem adapter = new RecyclerViewAdapterForMainitem(R.layout.main_item,getMovieList(new ArrayList<>()),context);

        switch (item.getZoneName()){
            case "电影":
                recyclerView.setAdapter(adapter);
                break;
            case "电视剧":
                adapter.setData(getTvList(new ArrayList<>()));
                recyclerView.setAdapter(adapter);
                break;
            case "综艺":
                adapter.setData(getVarietyList(new ArrayList<>()));
                recyclerView.setAdapter(adapter);
                break;
        }
    }

    /**
     * 电影节目数据
     * @param list
     * @return
     */
    private List<MovieInfo> getMovieList(List<MovieInfo> list){

        list.add(new MovieInfo(R.drawable.filmcover1,"唐人街探案3","5.6"));
        list.add(new MovieInfo(R.drawable.filmcover2,"你好,李焕英","8.1"));
        list.add(new MovieInfo(R.mipmap.coverys,"我不是药神","9.0"));
        list.add(new MovieInfo(R.mipmap.covercy,"星际穿越","9.3"));
        list.add(new MovieInfo(R.mipmap.coverhh,"红海行动","8.3"));
        list.add(new MovieInfo(R.mipmap.covernz,"哪吒之魔童降世","8.4"));
        list.add(new MovieInfo(R.mipmap.coversn,"少年的你","8.3"));

        return fixList(list);
    }

    /**
     * 电视剧节目数据
     * @param list
     * @return
     */
    private List<MovieInfo> getTvList(List<MovieInfo> list){

        list.add(new MovieInfo(R.mipmap.zx004,"赘婿","6.6"));
        list.add(new MovieInfo(R.mipmap.jx007,"觉醒年代","9.1"));
        list.add(new MovieInfo(R.mipmap.sh006,"山海情","9.4"));
        list.add(new MovieInfo(R.mipmap.xj002,"仙剑奇侠传三","8.7"));
        list.add(new MovieInfo(R.mipmap.ta003,"唐人街探案","7.3"));
        list.add(new MovieInfo(R.mipmap.nh005,"了不起的女孩","6.9"));
        list.add(new MovieInfo(R.mipmap.jy001,"机智牢房生活","9.4"));

        return fixList(list);
    }

    /**
     * 综艺节目数据
     * @param list
     * @return
     */
    private List<MovieInfo> getVarietyList(List<MovieInfo> list){

        list.add(new MovieInfo(R.mipmap.xt003,"乐队的夏天 第二季","7.4"));
        list.add(new MovieInfo(R.mipmap.yy004,"演员请就位 第二季","5.2"));
        list.add(new MovieInfo(R.mipmap.cz001,"创造101","5.9"));
        list.add(new MovieInfo(R.mipmap.wp002,"王牌对王牌 第四季","7.4"));
        list.add(new MovieInfo(R.mipmap.bz007,"国家宝藏 第三季","9.4"));
        list.add(new MovieInfo(R.mipmap.zg006,"追光吧！哥哥","4.9"));
        list.add(new MovieInfo(R.mipmap.sh005,"你好生活 第二季","8.9"));

        return fixList(list);
    }

    /**
     * 判断传进来的list,最大长度为7,否则抛出异常
     * @param list 要固定长度的list
     * @return 返回该list
     */
    private List<MovieInfo> fixList(List<MovieInfo> list){
        if (list.size() < 8){
            return list;
        }else {
            throw new UnsupportedOperationException("maxSize:7");
        }
    }
}
